package cn.edu.scau.cmi.tangqingning.hibernate;

import java.util.List;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;

public class HibernateController {

	@FXML
	private ComboBox<Teacher> cbTutor4s;
	@FXML
	private TextField tfName4t;
	@FXML
	private TextField tfName4s;
	@FXML
	private ListView<Student> lvStudent4s;
	@FXML
	private TextField tfId4s;
	@FXML
	private ListView<Teacher> lvTeacher4t;
	@FXML
	private ListView<Student> lvStudent4t;
	@FXML
	private TextField tfId4t;
	@FXML
	private TabPane tabPane;

	private StudentDAO studentDAO = new StudentDAO();
	private TeacherDAO teacherDAO = new TeacherDAO();

	private final Teacher NOTeacher = new Teacher(0, "绌�");

	private long lastClikeTime = 0;

	@FXML
	void initialize(){
		updateStudentList();
		updateTeacheList();

		lvStudent4t.setOnMouseClicked(ov->{
			if (System.currentTimeMillis() - lastClikeTime < 200) {
				Student student = lvStudent4t.getSelectionModel().getSelectedItem();
				if (student == null) {
					return;
				}
				lvStudent4s.getSelectionModel().select(student);
				tabPane.getSelectionModel().select(0);
				lastClikeTime = 0;
				return;
			}
			lastClikeTime = System.currentTimeMillis();
		});

		lvTeacher4t.getSelectionModel().selectedItemProperty().addListener(ov->{
			Teacher teacher = lvTeacher4t.getSelectionModel().getSelectedItem();
			if (teacher == null) {
				return;
			}
			lvStudent4t.getItems().setAll(studentDAO.findByProperty("teacher", teacher));
			tfId4t.setText(teacher.getId() + "");
			tfName4t.setText(teacher.getName());
		});

		lvStudent4s.getSelectionModel().selectedItemProperty().addListener(ov->{
			Student student = lvStudent4s.getSelectionModel().getSelectedItem();
			if (student == null) {
				return;
			}
			tfId4s.setText(student.getId() + "");
			tfName4s.setText(student.getName());
			cbTutor4s.getSelectionModel().select(student.getTeacher() != null ? student.getTeacher() : NOTeacher);
		});
	}

	@FXML
	void onAddClick4s(ActionEvent event){
		if (tfId4s.getText().length() == 0 || tfName4s.getText().length() == 0) {
			showAlert("缂栧彿鎴栧鍚嶆湁璇�");
			return;
		}
		Student student = new Student();
		student.setId(Integer.parseInt(tfId4s.getText()));
		student.setName(tfName4s.getText());
		student.setTeacher(cbTutor4s.getValue());
		try {
			studentDAO.save(student);
		} catch (Exception e) {
			// TODO: handle exception
			showAlert(e.getMessage());
		}
		updateStudentList();
	}

	@FXML
	void onAddClick4t(ActionEvent event){
		if (tfId4t.getText().length() == 0 || tfName4t.getText().length() == 0) {
			showAlert("缂栧彿鎴栧鍚嶆湁璇�");
			return;
		}
		Teacher teacher = new Teacher();
		teacher.setId(Integer.parseInt(tfId4t.getText()));
		teacher.setName(tfName4t.getText());
		try {
			teacherDAO.save(teacher);
		} catch (Exception e) {
			// TODO: handle exception
			showAlert(e.getMessage());
		}
		updateTeacheList();
	}

	@FXML
	void onUpdateClick4s(ActionEvent event){
		Student student = lvStudent4s.getSelectionModel().getSelectedItem();
		int id = 0;
		try {
			id = Integer.parseInt(tfId4s.getText());
		} catch (Exception e) {
			// TODO: handle exception
			showAlert(e.getMessage());
			return;
		}
		if (id != student.getId()) {
			showAlert("缂栧彿鏃犳硶淇敼");
			tfId4s.setText(student.getId() + "");
			return;
		}
		if (tfName4s.getText().length() == 0) {
			showAlert("璇疯緭鍏ュ鍚�");
			return;
		}
		student.setName(tfName4s.getText());
		student.setTeacher(cbTutor4s.getSelectionModel().getSelectedItem());
		studentDAO.save(student);
		updateStudentList();
	}

	@FXML
	void onUpdateClick4t(ActionEvent event){
		Teacher teacher = lvTeacher4t.getSelectionModel().getSelectedItem();
		int id = 0;
		try {
			id = Integer.parseInt(tfId4s.getText());
		} catch (Exception e) {
			// TODO: handle exception
			showAlert(e.getMessage());
			return;
		}
		if (id != teacher.getId()) {
			showAlert("缂栧彿鏃犳硶淇敼");
			tfId4t.setText(teacher.getId() + "");
			return;
		}
		if (tfName4s.getText().length() == 0) {
			showAlert("璇疯緭鍏ュ鍚�");
			return;
		}
		teacher.setName(tfName4t.getText());
		teacherDAO.save(teacher);
		updateTeacheList();
	}

	@FXML
	void onDeleteClick4s(ActionEvent event){
		Student student = lvStudent4s.getSelectionModel().getSelectedItem();
		studentDAO.delete(student);
		updateStudentList();
	}

	@FXML
	void onDeleteClick4t(ActionEvent event){
		Teacher teacher = lvTeacher4t.getSelectionModel().getSelectedItem();
		teacherDAO.delete(teacher);
		updateTeacheList();
	}

	private void showAlert(String message){
		Alert alert = new Alert(AlertType.ERROR);
		alert.setContentText(message);
		alert.show();
	}

	private void updateStudentList(){
		List<Student> list = studentDAO.findAll();
		lvStudent4s.getItems().clear();
		lvStudent4s.getItems().addAll(list);
	}

	private void updateTeacheList(){
		List<Teacher> list = teacherDAO.findAll();
		lvTeacher4t.getItems().setAll(list);
		cbTutor4s.getItems().clear();
		cbTutor4s.getItems().add(NOTeacher);
		cbTutor4s.getItems().addAll(list);
	}
}
